package com.vs.wms.login;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vs.wms.dao.AdminDao;
import com.vs.wms.dao.impl.AdminDaoImpl;
import com.vs.wms.pojo.Admin;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html; charset=utf-8");
		
		PrintWriter out = response.getWriter();
		
		String randNum = (String) request.getSession().getAttribute("rand");
		String randNumber = request.getParameter("randNumber");
		
		AdminDao adminDao = new AdminDaoImpl();
		
		if (randNumber.equals(randNum)) {
			
			String userName = request.getParameter("userName");
			String password = request.getParameter("userPass");
			
			Admin admin = adminDao.getAdminByUP(userName, password);
			
			if (admin != null) {
				
				// 设置登录用户名
				request.getSession().setAttribute("loginUser", admin);
				
				out.print("{success:true, msg:''}");
			}
			else {
				
				out.print("{success:false, msg:'用户名或者密码输入错误!'}");
			}
		}
		else {
			
			out.print("{success:false, msg:'验证码不正确, 请重新输入!'}");
		}
		
		out.flush();
		out.close();
	}
}